<?php

namespace app\common\model\publics;

use think\Model;
use think\Db;

class Report extends Model
{
	// 设置当前模型使用的完整数据库连接信息
	protected $connection = 'database.db_second';//连接配置
    // 表名
    protected $name = 'publics_report';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

	// 追加属性
	protected $append = [
		'images_url',
		'ss_images_url',
		'admin_name',
		'admin2_name',
		'nickname',
	    'avatar',
		'type_text',
		'state_text',
		'jb_state_text',
		'ss_state_text',
		'ss_time_text',
		'fk_time_text',
		'ss_cl_time_text',
		'jb_cl_time_text',
		'create_time_text',
		'identity'
	];

	public function getAvatarAttr($value, $data){
		$value = Db::name("user")->where(['id'=>$data['user_id']])->value('avatar');
		return $value ? $value : '';
	}
	
	public function getTypeList()
	{
		return ['1' => __('动态'), '2' => __('圈友'),'3' => __('吧主'), '4' => __('管理员')];
	}

	public function getStateList()
	{
		return ['1' => __('举报待处理'), '2' => __('申诉待处理'), '3' => __('举报已处理'), '4' => __('申诉已处理')];
	}

	public function getJbStateList()
	{
		return ['1' => __('举报失败'), '2' => __('违规警告'), '3' => __('移到黑名单'), '4' => __('降为圈友')];
	}

	public function getSsStateList()
	{
		return ['1' => '申诉审核中', '2' => '申诉成功','3'=>'申诉失败'];
	}


	public function getTypeTextAttr($value, $data)
	{
		$value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
		$list = $this->getTypeList();

		return isset($list[$value]) ? $list[$value] : '';
	}


	public function getStateTextAttr($value, $data)
	{
		$value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
		$list = $this->getStateList();
		return isset($list[$value]) ? $list[$value] : '';
	}


	public function getJbStateTextAttr($value, $data)
	{
		$value = $value ? $value : (isset($data['jb_state']) ? $data['jb_state'] : '');
		$list = $this->getJbStateList();
		return isset($list[$value]) ? $list[$value] : '';
	}


	public function getSsStateTextAttr($value, $data)
	{
		$value = $value ? $value : (isset($data['ss_state']) ? $data['ss_state'] : '');
		$list = $this->getSsStateList();
		return isset($list[$value]) ? $list[$value] : '';
	}


	public function getSsTimeTextAttr($value, $data)
	{
		$value = $value ? $value : (isset($data['ss_time']) ? $data['ss_time'] : '');
		return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
	}


	public function getFkTimeTextAttr($value, $data)
	{
		$value = $value ? $value : (isset($data['fk_time']) ? $data['fk_time'] : '');
		return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
	}

	public function getSsClTimeTextAttr($value, $data)
	{
		$value = $value ? $value : (isset($data['ss_cl_time']) ? $data['ss_cl_time'] : '');
		return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
	}

	public function getJbClTimeTextAttr($value, $data)
	{
		$value = $value ? $value : (isset($data['jb_cl_time']) ? $data['jb_cl_time'] : '');
		return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
	}


	public function getCreateTimeTextAttr($value, $data)
	{
		$value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
		return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
	}

	protected function setSsTimeAttr($value)
	{
		return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
	}

	protected function setFkTimeAttr($value)
	{
		return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
	}

	protected function setCreateTimeAttr($value)
	{
		return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
	}


	public function getAdminNameAttr($value, $data){

		$value = $data['admin_id'];//Db::name("user")->where(['id'=>$data['admin_id']])->value('nickname');

		return $value ? $value : '';

	}

	public function getAdmin2NameAttr($value, $data){

		$value = $data['admin2_id'];//Db::name("user")->where(['id'=>$data['admin2_id']])->value('nickname');

		return $value ? $value : '';

	}

	public function getNickNameAttr($value, $data){

		$value = Db::name("user")->where(['id'=>$data['jb_user_id']])->value('nickname');

		return $value ? $value : '';

	}

	//照片资料
	public function getImagesUrlAttr($value, $data){

		$value = $value ?: $data["images"];
		return getImagesUrl($value);

	}
	//申诉图片
	public function getSsImagesUrlAttr($value, $data){

		$value = $value ?: $data["ss_images"];
		return getImagesUrl($value);

	}

	/*
	 * 举报人
	 */
	public function user()
	{
		return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}

	/*
	 * 被举报人
	 */
	public function jbuser()
	{
		return $this->belongsTo('app\common\model\User', 'jb_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}

//	public function trends()
//	{
//		return $this->belongsTo('app\common\model\Trends', 'trends_id', 'id', [], 'LEFT')->setEagerlyType(0);
//	}
	public function getIdentityAttr($value, $data)
	{
		$value = Db::connect($this->connection)->name('circle_user')->where(['circle_id'=>$data['circle_id'],'user_id'=>$data['jb_user_id']])->value('identity');

		return $value ? $value : 0;
	}

}
